package com.personal.website.lottery.table

import java.util
import scala.collection.JavaConverters._

/**
 * Created by Gavin on 2015/12/16.
 */
/**
 * Created by Gavin on 2015/12/14.
 */
class EvenTable(issue: Int,redBalls: Seq[Int], blueBall: Int) extends Table(issue,redBalls,blueBall){
  val evenBalls = redBalls.filter(_ % 2 == 0)

  override def createRecord() = createEvens ++ createMainTable ++ createFrontSection ++
    createBackSection ++ createMissSection

  def createOddVsEven = s"${redBalls.size - evenBalls.size}:${evenBalls.size}"
  override def createSum(): Int = evenBalls.sum

  def createEvens = EvenTable.ALL_EXISTS_BALLS.map(ball => if(evenBalls.contains(ball)) ball else DEFAULT_NON_EXISTS_VALUE)
  def createMainTable = (1 to 4).map(digit => if(evenBalls.count(ball => if(ball % 8 == digit * 2 % 8) true else false) > 0) digit else DEFAULT_NON_EXISTS_VALUE)
  def createFrontSection = (1 to 4).map(digit => if(evenBalls.count(ball => if(ball < 17 && ball % 8 == digit * 2 % 8) true else false) > 0) digit else DEFAULT_NON_EXISTS_VALUE)
  def createBackSection = (1 to 4).map(digit => if(evenBalls.count(ball => if(ball > 17 && ball % 8 == digit * 2 % 8) true else false) > 0) digit else DEFAULT_NON_EXISTS_VALUE)
  def createMissSection = {
    (1 to 4).map { digit =>
      val actualDigit = (digit - 1) * 8 + 2
      if(Range(actualDigit, actualDigit + 7, 2).count(evenBalls.contains(_)) == 0) digit else DEFAULT_NON_EXISTS_VALUE
    }
  }
  def createFrontSection1 = Seq(1,3).map(digit => if(createFrontSection.filter(x => x != DEFAULT_NON_EXISTS_VALUE && x % 2 == 1).contains(digit)) digit else DEFAULT_NON_EXISTS_VALUE)
  def createBackSection1 = Seq(1,3).map(digit => if(createBackSection.filter(x => x != DEFAULT_NON_EXISTS_VALUE && x % 2 == 1).contains(digit)) digit else DEFAULT_NON_EXISTS_VALUE)
  def createFrontSection2 = Seq(2,4).map(digit => if(createFrontSection.filter(x => x != DEFAULT_NON_EXISTS_VALUE && x % 2 == 0).contains(digit)) digit else DEFAULT_NON_EXISTS_VALUE)
  def createBackSection2 = Seq(2,4).map(digit => if(createBackSection.filter(x => x != DEFAULT_NON_EXISTS_VALUE && x % 2 == 0).contains(digit)) digit else DEFAULT_NON_EXISTS_VALUE)
  def createRearrangeBalls = EvenTable.REARRANGE_BALLS.map(ball => if(evenBalls.contains(ball)) ball else DEFAULT_NON_EXISTS_VALUE)

  override def toString: String = Seq(Seq(issue),Seq(createOddVsEven),Seq(createSum()),createRecord()).flatten.mkString(";")
}

object EvenTable{
  def apply(issue: Int,redBalls: Seq[Int], blueBall: Int) = new EvenTable(issue,redBalls,blueBall)
  def apply(issue: Int,allBalls: Seq[Int]) = new EvenTable(issue,allBalls.init,allBalls.last)
  def newInstance(issue: Integer,redBalls: util.List[Integer],blueBall: Integer) = new EvenTable(issue.toInt, redBalls.asScala.map(_.toInt),blueBall.toInt)

  val ALL_EXISTS_BALLS = (1 to 33).filter(_ % 2 == 0)

  val REARRANGE_BALLS = Seq(2,10,6,14,18,26,22,30,4,12,8,16,20,28,24,32)

  val headString = Seq("issue","oddVsEven","sum",ALL_EXISTS_BALLS,"mainTable","frontSection","backSection","missSection","frontSection1","backSection1","frontSection2","backSection2","rearrangeBalls").mkString("|")

}

